# RUN: yaml2obj -D DESC='0d000000' -D MODE=1 -D HEAP=1 -D STACK=1 %s -o %t
# RUN: llvm-readelf --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=GNU,GNU-OK,ASYNC,HEAP,STACK
# RUN: llvm-readobj --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=LLVM,LLVM-OK,ASYNC,HEAP,STACK

# RUN: yaml2obj -D DESC='0e000000' -D MODE=0 -D HEAP=1 -D STACK=1 %s -o %t
# RUN: llvm-readelf --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=GNU,GNU-OK,SYNC,HEAP,STACK
# RUN: llvm-readobj --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=LLVM,LLVM-OK,SYNC,HEAP,STACK

# RUN: yaml2obj -D DESC='05000000' -D MODE=1 -D HEAP=1 -D STACK=0 %s -o %t
# RUN: llvm-readelf --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=GNU,GNU-OK,ASYNC,HEAP,NOSTACK
# RUN: llvm-readobj --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=LLVM,LLVM-OK,ASYNC,HEAP,NOSTACK

# RUN: yaml2obj -D DESC='06000000' -D MODE=0 -D HEAP=1 -D STACK=0 %s -o %t
# RUN: llvm-readelf --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=GNU,GNU-OK,SYNC,HEAP,NOSTACK
# RUN: llvm-readobj --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=LLVM,LLVM-OK,SYNC,HEAP,NOSTACK

# RUN: yaml2obj -D DESC='09000000' -D MODE=1 -D HEAP=0 -D STACK=1 %s -o %t
# RUN: llvm-readelf --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=GNU,GNU-OK,ASYNC,NOHEAP,STACK
# RUN: llvm-readobj --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=LLVM,LLVM-OK,ASYNC,NOHEAP,STACK

# RUN: yaml2obj -D DESC='0a000000' -D MODE=0 -D HEAP=0 -D STACK=1 %s -o %t
# RUN: llvm-readelf --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=GNU,GNU-OK,SYNC,NOHEAP,STACK
# RUN: llvm-readobj --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=LLVM,LLVM-OK,SYNC,NOHEAP,STACK

# RUN: yaml2obj -D DESC='03000000' -D MODE=2 -D HEAP=0 -D STACK=0 %s -o %t
# RUN: llvm-readelf --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=GNU,GNU-OK,UNKNOWN,NOHEAP,NOSTACK
# RUN: llvm-readobj --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=LLVM,LLVM-OK,UNKNOWN,NOHEAP,NOSTACK

# RUN: yaml2obj -D DESC='00000000' -D MODE=2 -D HEAP=0 -D STACK=0 %s -o %t
# RUN: llvm-readelf --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=GNU,GNU-OK,NONE,NOHEAP,NOSTACK
# RUN: llvm-readobj --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=LLVM,LLVM-OK,NONE,NOHEAP,NOSTACK

# RUN: yaml2obj -D DESC='""' -D MODE=2 -D HEAP=2 -D STACK=2 %s -o %t
# RUN: llvm-readelf --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=GNU,GNU-BAD,INVALID
# RUN: llvm-readobj --notes --dynamic --memtag %t | FileCheck %s --check-prefixes=LLVM,LLVM-BAD,INVALID

# LLVM: DynamicSection [ (6 entries)
# LLVM:  Tag                Type                     Name/Value
# GNU: Dynamic section
# GNU-SAME: contains 6 entries

# ASYNC:      0x0000000070000009
# ASYNC-SAME: AARCH64_MEMTAG_MODE
# ASYNC-SAME: Asynchronous (1)

# SYNC:      0x0000000070000009
# SYNC-SAME: AARCH64_MEMTAG_MODE
# SYNC-SAME: Synchronous (0)

# UNKNOWN:      0x0000000070000009
# UNKNOWN-SAME: AARCH64_MEMTAG_MODE
# UNKNOWN-SAME: Unknown (2)

# INVALID:      0x0000000070000009
# INVALID-SAME: AARCH64_MEMTAG_MODE
# INVALID-SAME: Unknown (2)

# HEAP:      0x000000007000000{{[bB]}}
# HEAP-SAME: AARCH64_MEMTAG_HEAP
# HEAP-SAME: Enabled (1)

# NOHEAP:      0x000000007000000{{[bB]}}
# NOHEAP-SAME: AARCH64_MEMTAG_HEAP
# NOHEAP-SAME: Disabled (0)

# INVALID:      0x000000007000000{{[bB]}}
# INVALID-SAME: AARCH64_MEMTAG_HEAP
# INVALID-SAME: Unknown (2)

# STACK:      0x000000007000000{{[cC]}}
# STACK-SAME: AARCH64_MEMTAG_STACK
# STACK-SAME: Enabled (1)

# NOSTACK:      0x000000007000000{{[cC]}}
# NOSTACK-SAME: AARCH64_MEMTAG_STACK
# NOSTACK-SAME: Disabled (0)

# INVALID:      0x000000007000000{{[cC]}}
# INVALID-SAME: AARCH64_MEMTAG_STACK
# INVALID-SAME: Unknown (2)

# LLVM:  0x000000007000000D AARCH64_MEMTAG_GLOBALS   0xdeadbeef
# LLVM:  0x000000007000000F AARCH64_MEMTAG_GLOBALSSZ 1234

# GNU: 0x000000007000000d (AARCH64_MEMTAG_GLOBALS)   0xdeadbeef0
# GNU: 0x000000007000000f (AARCH64_MEMTAG_GLOBALSSZ) 1234

# GNU:          Displaying notes found in: .note.android.memtag
# GNU-NEXT:     Owner    Data size	 Description
# GNU-OK-NEXT:  Android  0x00000004  NT_ANDROID_TYPE_MEMTAG (Android memory tagging information)
# GNU-BAD-NEXT: Android  0x00000000  NT_ANDROID_TYPE_MEMTAG (Android memory tagging information)

# LLVM:      Notes [
# LLVM-NEXT:   NoteSection {
# LLVM-NEXT:     Name: .note.android.memtag
# LLVM-NEXT:     Offset: 0x40
# LLVM-OK-NEXT:  Size: 0x18
# LLVM-BAD-NEXT: Size: 0x14
# LLVM-NEXT:     Note {
# LLVM-NEXT:       Owner: Android
# LLVM-OK-NEXT:    Data size: 0x4
# LLVM-BAD-NEXT:   Data size: 0x0
# LLVM-NEXT:       Type: NT_ANDROID_TYPE_MEMTAG (Android memory tagging information)

## Hint: Also used for the GNU tests.
# INVALID-NEXT:    Invalid .note.android.memtag
# NONE-NEXT:       Tagging Mode: NONE
# ASYNC-NEXT:      Tagging Mode: ASYNC
# SYNC-NEXT:       Tagging Mode: SYNC
# UNKNOWN-NEXT:    Tagging Mode: Unknown (3)
# HEAP-NEXT:       Heap: Enabled
# NOHEAP-NEXT:     Heap: Disabled
# STACK-NEXT:      Stack: Enabled
# NOSTACK-NEXT:    Stack: Disabled

# LLVM-NEXT:     }
# LLVM-NEXT:   }
# LLVM-NEXT: ]

# LLVM: Memtag Dynamic Entries
# GNU:  Memtag Dynamic Entries

## Ensure that for --memtag, we don't print irrelevant dynamic entries.
# LLVM-NOT: DT_INIT_ARRAY
# GNU-NOT: DT_INIT_ARRAY

# SYNC:    AARCH64_MEMTAG_MODE: Synchronous (0)
# ASYNC:   AARCH64_MEMTAG_MODE: Asynchronous (1)
# HEAP:    AARCH64_MEMTAG_HEAP: Enabled (1)
# NOHEAP:  AARCH64_MEMTAG_HEAP: Disabled (0)
# STACK:   AARCH64_MEMTAG_STACK: Enabled (1)
# NOSTACK: AARCH64_MEMTAG_STACK: Disabled (0)
# LLVM:    AARCH64_MEMTAG_GLOBALS: 0xdeadbeef0
# GNU:     AARCH64_MEMTAG_GLOBALS: 0xdeadbeef0
# LLVM:    AARCH64_MEMTAG_GLOBALSSZ: 1234
# GNU:     AARCH64_MEMTAG_GLOBALSSZ: 1234

# LLVM-OK: Memtag Android Note
# GNU-OK:  Memtag Android Note

# SYNC:    Tagging Mode: SYNC
# ASYNC:   Tagging Mode: ASYNC
# UNKNOWN: Tagging Mode: Unknown (3)
# HEAP:    Heap: Enabled
# NOHEAP:  Heap: Disabled
# STACK:   Stack: Enabled
# NOSTACK: Stack: Disabled

#########################################
## --docnum=1 (default)
#########################################

--- !ELF
FileHeader:
  Class:   ELFCLASS64
  Data:    ELFDATA2LSB
  Type:    ET_DYN
  Machine: EM_AARCH64
Sections:
  - Name: .note.android.memtag
    Type: SHT_NOTE
    Notes:
      - Name: Android
        Type: NT_ANDROID_TYPE_MEMTAG
        Desc: [[DESC]]
  - Name: .dynamic
    Type: SHT_DYNAMIC
    Entries:
      - Tag:   DT_AARCH64_MEMTAG_MODE
        Value: [[MODE]]
      - Tag:   DT_AARCH64_MEMTAG_HEAP
        Value: [[HEAP]]
      - Tag:   DT_AARCH64_MEMTAG_STACK
        Value: [[STACK]]
      - Tag:   DT_AARCH64_MEMTAG_GLOBALS
        Value: 0xdeadbeef0
      - Tag:   DT_AARCH64_MEMTAG_GLOBALSSZ
        Value: 1234
      - Tag:   DT_INIT_ARRAY
        Value: 0x1000

# RUN: yaml2obj --docnum=2 %s -o %t
# RUN: llvm-readelf --memtag %t | FileCheck %s --check-prefixes=MISSING-GNU
# RUN: llvm-readobj --memtag %t | FileCheck %s --check-prefixes=MISSING-LLVM

## Ensure the header is printed, even if there's no relevant dynamic entries,
## and that nothing else is printed.

# MISSING-GNU-NOT:  {{.}}
# MISSING-GNU:      Memtag Dynamic Entries:
# MISSING-GNU-NEXT: < none found >
# MISSING-GNU-NOT:  {{.}}

# MISSING-LLVM-NOT:  {{.}}
# MISSING-LLVM:      File: {{.*}}memtag.test
# MISSING-LLVM-NEXT: Format: elf64-littleaarch64
# MISSING-LLVM-NEXT: Arch: aarch64
# MISSING-LLVM-NEXT: AddressSize: 64bit
# MISSING-LLVM-NEXT: LoadName:
# MISSING-LLVM-NEXT: Memtag Dynamic Entries: [
# MISSING-LLVM-NEXT:   < none found >
# MISSING-LLVM-NEXT: ]
# MISSING-LLVM-NOT:  {{.}}

#########################################
## --docnum=2
#########################################

--- !ELF
FileHeader:
  Class:   ELFCLASS64
  Data:    ELFDATA2LSB
  Type:    ET_DYN
  Machine: EM_AARCH64
Sections:
  - Name: .dynamic
    Type: SHT_DYNAMIC
    Entries:
      - Tag:   DT_INIT_ARRAY
        Value: 0x1000
